package com.label.grade.service.impl;/**
 * @author zhf
 * @date 2023/7/9 17:14
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.label.grade.entity.Do.ClassInfo;
import com.label.grade.entity.Do.Dictionary;
import com.label.grade.entity.Do.UndergraInfo;
import com.label.grade.entity.Vo.AcademicAlertClassCompareVo;
import com.label.grade.entity.Vo.AcademicAlertInfoVo;
import com.label.grade.entity.Vo.AcademicAlertVo;
import com.label.grade.entity.Vo.ClassGradeInfoVo;
import com.label.grade.mapper.AcademicAlertMapper;
import com.label.grade.mapper.ClassInfoMapper;
import com.label.grade.mapper.DictionaryMapper;
import com.label.grade.mapper.UndergraInfoMapper;
import com.label.grade.service.AcademicAlertService;
import com.label.grade.utils.ExamSemesterCalculatorUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @author zhf
 * 项目：student
 * 描述：
 * @date 2023/7/9 17:14
 **/
@Service
public class AcademicAlertServiceImpl implements AcademicAlertService {

    @Resource
    private AcademicAlertMapper academicAlertMapper;

    @Resource
    private UndergraInfoMapper undergraInfoMapper;

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Resource
    private ClassInfoMapper classInfoMapper;


    @Override
    public List<AcademicAlertVo> getAcademicAlertAll(String teacherCode) {

        List<AcademicAlertVo> academicAlertVos = academicAlertMapper.listAcademicAlertInfo(teacherCode).stream().filter(academicAlertVo -> !academicAlertVo.getAlertLevel().equals(0)).collect(Collectors.toList());

        return academicAlertVos;
    }

    @Override
    public AcademicAlertInfoVo getAcademicAlertInfo(String studentCode) {

        AcademicAlertInfoVo academicAlertInfoVo = new AcademicAlertInfoVo();
        //1 拿到一个学生所有挂科数据
        List<ClassGradeInfoVo> classGradeInfoVos = academicAlertMapper.selFailedClassInfo(studentCode);
        classGradeInfoVos.forEach(classGradeInfoVo -> {
            classGradeInfoVo.setAcademicSemester(dictionaryMapper
                    .selectOne(new QueryWrapper<Dictionary>()
                            .lambda()
                            .eq(Dictionary::getLabel,"year")
                            .eq(Dictionary::getDicCode,ExamSemesterCalculatorUtil
                                    .main("20" + studentCode.substring(0,2),classGradeInfoVo.getAcademicSemester())
                                    .get("academicYear")-1))
                    .getDicValue()
                    .concat( dictionaryMapper
                            .selectOne(new QueryWrapper<Dictionary>()
                                    .lambda()
                                    .eq(Dictionary::getLabel,"semester")
                                    .eq(Dictionary::getDicCode,ExamSemesterCalculatorUtil
                                            .main("20" + studentCode.substring(0,2),classGradeInfoVo.getAcademicSemester())
                                            .get("semester")-1))
                            .getDicValue()));

        });
        academicAlertInfoVo.setClassInfo(classGradeInfoVos);
        UndergraInfo undergraInfo = undergraInfoMapper.selectOne(new QueryWrapper<UndergraInfo>().lambda().eq(UndergraInfo::getStuNumber,studentCode));
        academicAlertInfoVo.setClassLabel(undergraInfo.getClassName());
        academicAlertInfoVo.setMajorLabel(undergraInfo.getMajor());
        academicAlertInfoVo.setEnrollmentYear(undergraInfo.getStuNumber().substring(0,2));
        academicAlertInfoVo.setParentName(undergraInfo.getParentName());
        academicAlertInfoVo.setParentNumber(undergraInfo.getParentContactNumber());
        academicAlertInfoVo.setStudentCode(undergraInfo.getStuNumber());
        academicAlertInfoVo.setStudentName(undergraInfo.getStuName());
        academicAlertInfoVo.setStudentNumber(undergraInfo.getContactNumber());

        return academicAlertInfoVo;
    }

    @Override
    public List<AcademicAlertClassCompareVo> getAcademicAlertClassCompare(String teacherCode) {
        List<AcademicAlertClassCompareVo> classCompareVos = new ArrayList<>();

        //1 获取当前老师对应的所带班级 专业 年级 信息（班级信息表）
        List<ClassInfo> classInfos = classInfoMapper.selectList(new QueryWrapper<ClassInfo>().lambda().eq(ClassInfo::getInstructorNumber,teacherCode));

        classInfos.forEach(classInfo -> {
            AcademicAlertClassCompareVo classCompareVo = new AcademicAlertClassCompareVo();
            //先设置要返回的基本信息（无需计算）
            classCompareVo.setClassLabel(classInfo.getClassName());
            classCompareVo.setMajorLabel(classInfo.getMajor());
            classCompareVo.setYear(classInfo.getEnrollment());
            //2 根据班级 获取对应班级的学生（数量totalClassSize）（基本信息表）
            List<Object> studentCodeObjects = undergraInfoMapper.selectObjs(new QueryWrapper<UndergraInfo>().lambda().eq(UndergraInfo::getClassName,classInfo.getClassName()).select(UndergraInfo::getStuNumber));
            //2.1 转为List<String>
            List<String> studentCodes = studentCodeObjects.stream().map(Object::toString).collect(Collectors.toList());
            //2.2 统计班级学生总数
            classCompareVo.setTotalClassSize(studentCodes.size());
            //2.3 拿出每个学号 计算它的预警等级
            AtomicReference<Integer> alert1Temp = new AtomicReference<>(0);
            AtomicReference<Integer> alert2Temp = new AtomicReference<>(0);
            AtomicReference<Integer> alert3Temp = new AtomicReference<>(0);
            studentCodes.forEach(studentCode -> {
                //3 查找每个学生的预警等级count++
                AcademicAlertVo academicAlertVo = academicAlertMapper.selAcademicAlertInfo(studentCode);
                //3.1 如果该学生没有挂科则可能返回空
                if(academicAlertVo == null) return;
                if(academicAlertVo.getAlertLevel() == 1){
                    alert1Temp.getAndSet(alert1Temp.get() + 1);
                }else if (academicAlertVo.getAlertLevel() == 2) {
                    alert2Temp.getAndSet(alert2Temp.get() + 1);
                }else if (academicAlertVo.getAlertLevel() == 3) {
                    alert3Temp.getAndSet(alert3Temp.get() + 1);
                }
            });
            //4 计算占比
            Integer alert1 = alert1Temp.get();
            Integer alert2 = alert2Temp.get();
            Integer alert3 = alert3Temp.get();
            classCompareVo.setAlert1(String.valueOf((alert1*100)/classCompareVo.getTotalClassSize()).concat("%"));
            classCompareVo.setAlert2(String.valueOf((alert2*100)/classCompareVo.getTotalClassSize()).concat("%"));
            classCompareVo.setAlert3(String.valueOf((alert3*100)/classCompareVo.getTotalClassSize()).concat("%"));

            //5 填充数据
            classCompareVos.add(classCompareVo);
        });

        return classCompareVos;
    }
}
